package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.adapter.EmptyRecyclerView;
import cn.edu.neusoft.wuguangsheng.flowerimpression.adapter.GoodsCommentAdapter;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.GoodsBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.GoodsCommentBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.ListListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.AddGoodsCartModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.CollectionGoodsModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetCollectionGoodsFlagModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetGoodsCommentModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetGoodsInfoModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.RemoveCollectionGoodsModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.service.Service;

public class GoodsInfoActivity extends BaseActivity implements View.OnClickListener{

    String good_id;
    ImageView goodsinfo_fh,goodsinfo_img;
    ImageView goodsinfo_shoucang;
    TextView goodsinfo_price,goodsinfo_name,goodsinfo_kucun,goodsinfo_intro,goodsinfo_guige;
    TextView goodsinfo_ljgm,goodsinfo_jrgwc,goodsinfo_lxsj;
    View goodsinfo_emptyview;
    EmptyRecyclerView goodsinfo_rv;
    private int shoucangFlag;
    private Dialog dialog;
    boolean up = false;
    int sum;
    int i=1;
    double price;
    String shop_name;
    String shop_phone;
    TextView goodsinfo_add,goodsinfo_desc,goodsinfo_num;
    TextView tv_message;

    private List<GoodsCommentBean> items;
    @Override
    void initView() {
        setLayout_file(R.layout.activity_goods_info);
        goodsinfo_fh = (ImageView)findViewById(R.id.goodsinfo_fh);
        goodsinfo_img = (ImageView)findViewById(R.id.goodsinfo_img);
        goodsinfo_lxsj = (TextView)findViewById(R.id.goodsinfo_lxsj);
        tv_message = (TextView)findViewById(R.id.tv_message);

        goodsinfo_price = (TextView)findViewById(R.id.goodsinfo_price);
        goodsinfo_name = (TextView)findViewById(R.id.goodsinfo_name);
        goodsinfo_kucun = (TextView)findViewById(R.id.goodsinfo_kucun);
        goodsinfo_intro = (TextView)findViewById(R.id.goodsinfo_intro);
        goodsinfo_guige = (TextView)findViewById(R.id.goodsinfo_guige);

        goodsinfo_add = (TextView)findViewById(R.id.goodsinfo_add);
        goodsinfo_desc = (TextView)findViewById(R.id.goodsinfo_desc);
        goodsinfo_num = (TextView)findViewById(R.id.goodsinfo_num);

        goodsinfo_ljgm = (TextView)findViewById(R.id.goodsinfo_ljgm);
        goodsinfo_jrgwc = (TextView)findViewById(R.id.goodsinfo_jrgwc);

        goodsinfo_shoucang = (ImageView) findViewById(R.id.goodsinfo_shoucang);

        goodsinfo_rv = (EmptyRecyclerView)findViewById(R.id.goodsinfo_rv);
        goodsinfo_emptyview = (View)findViewById(R.id.goodsinfo_emptyview);

        good_id = getIntent().getStringExtra("goodid");
        dialog = new WeiboDialogUtils().createLoadingDialog(GoodsInfoActivity.this,"加载中...");

        GetGoodsInfoModel getGoodsInfoModel = new GetGoodsInfoModel();
        getGoodsInfoModel.getGoodsInfo(good_id,goodsBeanTListener);

        GetCollectionGoodsFlagModel getCollectionGoodsFlagModel = new GetCollectionGoodsFlagModel();
        getCollectionGoodsFlagModel.getCollectionGoodsFlag(getid(),good_id,getCollectionGoodsFlagListener);

        GetGoodsCommentModel getGoodsCommentModel = new GetGoodsCommentModel();
        getGoodsCommentModel.getGoodsCommentList(good_id, new ListListener<GoodsCommentBean>() {
            @Override
            public void onResponse(List<GoodsCommentBean> list) {
                LinearLayoutManager linearLayoutManager=new LinearLayoutManager(GoodsInfoActivity.this);
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                goodsinfo_rv.setLayoutManager(linearLayoutManager);
                items=list;
                GoodsCommentAdapter goodsCommentAdapter=new GoodsCommentAdapter(GoodsInfoActivity.this,items,R.layout.knowcomment_item);
                goodsinfo_rv.setAdapter(goodsCommentAdapter);
                goodsinfo_rv.setItemAnimator(new DefaultItemAnimator());
                goodsinfo_rv.setEmptyView(goodsinfo_emptyview);
                WeiboDialogUtils.closeDialog(dialog);
                tv_message.setText("该商品还没有任何评论呢");
            }

            @Override
            public void onFail(String msg) {
                WeiboDialogUtils.closeDialog(dialog);
            }
        });
    }

    @Override
    void initEvent() {
   goodsinfo_add.setOnClickListener(this);
   goodsinfo_desc.setOnClickListener(this);
   goodsinfo_fh.setOnClickListener(this);
   goodsinfo_ljgm.setOnClickListener(this);
   goodsinfo_jrgwc.setOnClickListener(this);
   goodsinfo_shoucang.setOnClickListener(this);
        goodsinfo_lxsj.setOnClickListener(this);
    }

    @Override
    void initDate() {

    }

    TListener<SuccessBean> getCollectionGoodsFlagListener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {

            if (successBean.getSuccess().equals("1")){
                goodsinfo_shoucang.setImageResource(R.mipmap.shoucangl);
                shoucangFlag = 1;
            }
            else{
                goodsinfo_shoucang.setImageResource(R.mipmap.shoucang);
                shoucangFlag = 0;
            }
        }

        @Override
        public void onFail(String msg) {
            goodsinfo_shoucang.setImageResource(R.mipmap.shoucang);
            shoucangFlag = 0;
        }
    };
    TListener<GoodsBean> goodsBeanTListener = new TListener<GoodsBean>() {
        @Override
        public void onResponse(GoodsBean goodsBean) {
            String name = goodsBean.getGood_name();
             price = goodsBean.getGood_price();
            String intro = goodsBean.getGood_intro();
            String guige = goodsBean.getGood_norms();
             sum = goodsBean.getGood_num();
             shop_name = goodsBean.getShop_name();
            shop_phone = goodsBean.getShop_phone();
            String img = Service.BASE_URL+"flower_impression/public/uploads/"+goodsBean.getGood_photo();
//Toast.makeText(GoodsInfoActivity.this,shop_name,Toast.LENGTH_SHORT).show();
            goodsinfo_name.setText(name);
            goodsinfo_price.setText("￥"+price);
            goodsinfo_guige.setText(guige);
            goodsinfo_kucun.setText(String.valueOf(sum));
            goodsinfo_intro.setText(intro);
            goodsinfo_ljgm.setText("立即购买"+"(￥"+String.valueOf(price)+")");
            Picasso.with(GoodsInfoActivity.this).load(img).into(goodsinfo_img);

        }

        @Override
        public void onFail(String msg) {
            WeiboDialogUtils.closeDialog(dialog);
        }
    };

    private void add(){
       if (i<sum){
           i++;
           double allprice = i*price;
           goodsinfo_ljgm.setText("立即购买"+"(￥"+String.valueOf(allprice)+")");
           goodsinfo_num.setText(String.valueOf(i));
       }
       else {
           Toast.makeText(GoodsInfoActivity.this,"库存不够",Toast.LENGTH_SHORT).show();
       }
    }

    private void desc(){
       if (i>1){
           i--;
           double allprice = i*price;
           goodsinfo_ljgm.setText("立即购买"+"(￥"+String.valueOf(allprice)+")");
           goodsinfo_num.setText(String.valueOf(i));
       }
       else{
           Toast.makeText(GoodsInfoActivity.this,"最少需要购买一份",Toast.LENGTH_SHORT).show();
       }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.goodsinfo_add:
                add();
                break;
            case R.id.goodsinfo_desc:
                desc();
                break;
            case R.id.goodsinfo_fh:
                finish();
                break;
            case R.id.goodsinfo_ljgm:
                Intent intent = new Intent(GoodsInfoActivity.this,BuyOneGoodsActivity.class);
                intent.putExtra("goodid",good_id);
                intent.putExtra("shopname",shop_name);
                intent.putExtra("goodsnum",i);
                startActivity(intent);
                break;
            case R.id.goodsinfo_jrgwc:
                String userid = getid();
                String goodnum = String.valueOf(i);
                AddGoodsCartModel addGoodsCartModel = new AddGoodsCartModel();
                addGoodsCartModel.addGoodsCart(userid,good_id,goodnum,successBeanTListener);
                break;
            case R.id.goodsinfo_shoucang:
                if (shoucangFlag == 1){
                    //取消
                    RemoveCollectionGoodsModel removeCollectionGoodsModel = new RemoveCollectionGoodsModel();
                    removeCollectionGoodsModel.removeCollectionGoods(getid(),good_id,removeCollectionGoodsListener);
                }
                else if (shoucangFlag == 0){
                    //添加
                    CollectionGoodsModel collectionGoodsModel = new CollectionGoodsModel();
                    collectionGoodsModel.collectionGoods(getid(),good_id,collectionGoodsListener);
                }
                break;
            case R.id.goodsinfo_lxsj:

                AlertDialog alertDialog=new AlertDialog.Builder(GoodsInfoActivity.this)
                        .setIcon(R.drawable.video_icon)
                        .setTitle("温馨提示")
                        .setMessage("您确认要联系商家吗？")
                        .setNegativeButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //创建打电话的意图
                                Intent intents = new Intent();
                                //设置拨打电话的动作
                                intents.setAction(Intent.ACTION_CALL);
                                //设置拨打电话的号码
                                intents.setData(Uri.parse("tel:" + shop_phone));
                                //开启打电话的意图
                                startActivity(intents);
                            }
                        })
                        .setPositiveButton("取消",null)
                        .create();
                alertDialog.show();
                break;
        }

    }
    TListener<SuccessBean> removeCollectionGoodsListener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
            if (successBean.getSuccess().equals("1")){
                goodsinfo_shoucang.setImageResource(R.mipmap.shoucang);
                shoucangFlag = 0;
                Toast.makeText(GoodsInfoActivity.this,"取消收藏成功",Toast.LENGTH_SHORT).show();
            }
            else{
                goodsinfo_shoucang.setImageResource(R.mipmap.shoucang);
                shoucangFlag = 0;
                Toast.makeText(GoodsInfoActivity.this,"取消收藏失败",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(GoodsInfoActivity.this,"取消失败",Toast.LENGTH_SHORT).show();
        }
    };
    TListener<SuccessBean> collectionGoodsListener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
            if (successBean.getSuccess().equals("1")){
                goodsinfo_shoucang.setImageResource(R.mipmap.shoucangl);
                shoucangFlag = 1;
                Toast.makeText(GoodsInfoActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();
            }
            else{
                goodsinfo_shoucang.setImageResource(R.mipmap.shoucang);
                shoucangFlag = 0;
                Toast.makeText(GoodsInfoActivity.this,"收藏失败",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            goodsinfo_shoucang.setImageResource(R.mipmap.shoucang);
            shoucangFlag = 0;
            Toast.makeText(GoodsInfoActivity.this,"收藏失败",Toast.LENGTH_SHORT).show();
        }
    };

    TListener<SuccessBean> successBeanTListener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
            if (successBean.getSuccess().equals("1")){
                Toast.makeText(GoodsInfoActivity.this,"添加购物车成功",Toast.LENGTH_SHORT).show();

            }
            else if (successBean.getSuccess().equals("2")){
                Toast.makeText(GoodsInfoActivity.this,"该商品已添加购物车，不能重复添加",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(GoodsInfoActivity.this,"添加失败",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(GoodsInfoActivity.this,"发生错误",Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        up = true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (up) {
            initView();
            initEvent();
            initDate();
            up = false;
        }
    }


}
